<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>第一个OC程序 | </title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../3.html" />
    
    
    <link rel="prev" href="../occ/README.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="2" data-basepath=".." data-revision="1442762455528">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Objective-C简介
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="occ/README.html">
            
                
                    <a href="../occ/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         OC和C对比
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="2" data-path="oc/README.html">
            
                
                    <a href="../oc/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         第一个OC程序
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="3.html">
            
                
                    <a href="../3.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         面向对象思想
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="4.html">
            
                
                    <a href="../4.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         类与对象
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="5.html">
            
                
                    <a href="../5.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         类的设计
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="6.html">
            
                
                    <a href="../6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         第一个OC类
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="7.html">
            
                
                    <a href="../7.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         对象方法的声明和实现
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="8.html">
            
                
                    <a href="../8.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         类方法的声明和实现
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="9.html">
            
                
                    <a href="../9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         OC类练习
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="10.html">
            
                
                    <a href="../10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         对象的存储细节
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="11.html">
            
                
                    <a href="../11.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         函数与方法对比
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         常见错误
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="13.html">
            
                
                    <a href="../13.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="14" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                         作业
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" ></a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_54">
                    
                        <h1 id="oc">第一个OC程序</h1>
<h2 id="">本小节知识点</h2>
<ol>
<li>如何创建Objective-C项目</li>
<li>#import和#include区别</li>
<li>NSLog 和printf区别</li>
<li>“@”的使用方法</li>
<li>NS前缀</li>
</ol>
<hr>
<h2 id="1objective-c">1.如何创建Objective-C项目</h2>
<ul>
<li><p>创建工程
<img src="../images/a2/Snip20150512_3.png" alt="">
<img src="../images/a2/Snip20150512_4.png" alt="">
<img src="../images/a2/Snip20150527_8.png" alt="">
<img src="../images/a2/Snip20150527_9.png" alt=""></p>
</li>
<li><p>查看结果运行结果
<img src="../images/a2/Snip20150527_11.png" alt=""></p>
</li>
</ul>
<hr>
<h2 id="2importinclude">2.#import和#include区别</h2>
<ul>
<li>#import与#include的类似，都是把其后面的文件拷贝到该指令所在的地方</li>
<li>#import可以自动防止重复导入</li>
<li>#import &lt;&gt; 用于包含系统文件</li>
<li><p>#import&quot;&quot;用于包含本项目中的文件</p>
</li>
<li><p>#import <Foundation/Foundation.h>, 告诉编译器找到并处理名为Foundation.h文件,这是一个系统文件,#import表示将该文件的信息导入到程序中。</p>
</li>
<li><p>框架地址: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneO S.sdk/System/Library/Frameworks/</p>
</li>
</ul>
<hr>
<h2 id="3nslog-printf">3.NSLog 和printf区别</h2>
<ul>
<li><p>NSLog是Foundation框架􏰀供的Objective-C日志输出函数,与标准C中的printf函数类似,并可以格式化输出。</p>
<ul>
<li>NSLog传递进去的格式化字符是NSString的对象,而不是char *这种字符串指针</li>
<li>NSLog输出的内容中会自动包含一些系统信息</li>
<li>NSLog输出的内容会自动换行</li>
</ul>
</li>
<li><p>NSLog声明在NSObjCRuntime.h中</p>
<ul>
<li>声明：void NSLog(NSString *format, ...);</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc">NSLog(@“this is a test”); //打印一个字符串
NSString *str = @&quot;hello xiaomage!”;
NSLog(@&quot;string is:%@&quot;,str);//使用占位符,%@表示打印一个对象,%@ OC特有的
NSLog(@&quot;x=%d, y=%d&quot;,10,20);//使用多个占位符,%d表示整型数
</code></pre>
<hr>
<h2 id="4">4.“@”的使用方法</h2>
<ul>
<li>在OC中“@”有特殊的用法<ul>
<li>@&quot;&quot; 这个符号表示将一个C的字符串转化为OC中的字符串对象NSString.</li>
<li>@符号 OC中大部分的关键字都是以@开头的,比如@interface,@implementation,@end @class等。</li>
</ul>
</li>
</ul>
<hr>
<h2 id="5ns">5.NS前缀</h2>
<ul>
<li>NS来自于NeXTStep的一个软件 NeXT Software</li>
<li>OC中不支持命名空间(namespace)</li>
<li>NS是为了避免命名冲突而给的前缀</li>
<li>看到NS前缀就知道是Cocoa中的系统类的名称</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../occ/README.html" class="navigation navigation-prev " aria-label="Previous page: OC和C对比"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../3.html" class="navigation navigation-next " aria-label="Next page: 面向对象思想"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
